#!/bin/bash
set -e

source $(dirname $0)/version

cd $(dirname $0)/..

if [[ -z ${ORG} ]] || [[ -z ${REPO} ]] || [[ -z ${TAG} ]] || [[ -z ${RUNTIME} ]]; then
    echo "Usage: [REGISTRY=] ORG= REPO= TAG= RUNTIME= ./scripts/package"
    exit 1
fi

IMAGE=${ORG}/${REPO}:${TAG}
if [[ -n ${REGISTRY} ]]; then
    IMAGE=${REGISTRY%/}/${IMAGE}
fi
DOCKERFILE=package/Dockerfile
if [ -e ${DOCKERFILE}.${ARCH} ]; then
    DOCKERFILE=${DOCKERFILE}.${ARCH}
fi

BUILD="${RUNTIME} build -f ${DOCKERFILE} -t ${IMAGE}"

if [[ ${RUNTIME} == "podman" ]]; then
    BUILD="$BUILD -v /var/lib/containers:/var/lib/containers ."
else
    BUILD="$BUILD ."
fi

$BUILD
echo Built ${IMAGE}
